package com.inspur.icity.bus.restapi;

import com.inspur.icity.bus.api.user.enumeration.AreaCodeEnum;
import com.inspur.icity.bus.api.user.model.BusBaseData;
import com.inspur.icity.bus.api.user.model.BusRouteHistory;
import com.inspur.icity.bus.api.user.service.BusBaseDataService;
import com.inspur.icity.bus.api.user.service.BusRouteHistoryService;
import com.inspur.icity.bus.base.core.gaoderesult.AddressComponentResult;
import com.inspur.icity.bus.base.core.gaoderesult.GaocodeRegeoResult;
import com.inspur.icity.bus.base.core.gaoderesult.RegeocodesResult;
import com.inspur.icity.bus.base.core.model.BaseResult;
import com.inspur.icity.bus.base.core.util.GaodeMapApi;
import com.inspur.icity.bus.base.core.web.BaseController;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 首页Controller
 */
@Controller
@RequestMapping(value = "/home")
public class BusHomeController extends BaseController {

    @Autowired
    private BusRouteHistoryService busRouteHistoryService;

    @Autowired
    private BusBaseDataService busBaseDataService;

    /**
     * 首页获取数据：①当前城市、②我的位置、③登陆用户常用路线列表
     * @param longitude
     * @param latitude
     * @param mobile
     * @return
     */
    @RequestMapping(value = "/index",method = RequestMethod.GET)
    @ResponseBody
    public Object index(@RequestParam(value = "mobile",required = true) String mobile,
                       @RequestParam(value = "longitude",required = true) String longitude,
                       @RequestParam(value = "latitude",required = true) String latitude,
                       @RequestParam(value = "selectCityCode",required = false) String selectCityCode){
        Map<String,Object> resultMap = new HashMap<>();
        //高德逆地理编码
        GaocodeRegeoResult gaocodeRegeoResult = GaodeMapApi.geoRegeo(longitude,latitude);
        if(null != gaocodeRegeoResult && "1".equals(gaocodeRegeoResult.getStatus())){ //高德数据请求成功，进行数据封装
            RegeocodesResult regeocodesResult = gaocodeRegeoResult.getRegeocode();
            String city = regeocodesResult.getAddressComponent().getCity().toString();
            resultMap.put("positionCity", city); //当前城市
            resultMap.put("currentAddress",  captureAddress(regeocodesResult)); //我的当前位置
        }
        Map<String,Object> searchMap = new HashMap<>();
        searchMap.put("cityCode",selectCityCode);
        List<BusBaseData> baseList = busBaseDataService.find(searchMap);
        if(null != baseList && baseList.size() != 0){
            resultMap.put("dataSource",baseList.get(0).getCityBus());
            resultMap.put("currentCity", baseList.get(0).getCity()); //当前城市
            resultMap.put("areacode", baseList.get(0).getCode()); //当前城市公交系统代码

        }
        //获取常用路线列表
        List<BusRouteHistory> list = busRouteHistoryService.getBusRouteHistoryList(mobile,resultMap.get("areacode").toString());
        List<Map<String,Object>> resultList = new ArrayList<>();
        Map<String,Object> map = null;
        for (BusRouteHistory busRouteHistory : list){
            map = new HashMap<>();
            map.put("name",busRouteHistory.getName());
            map.put("direction",busRouteHistory.getDirection());
            map.put("areacode",busRouteHistory.getAreacode());
            map.put("updown",busRouteHistory.getUpdown());
            resultList.add(map);
        }
        resultMap.put("usedRouteData", resultList);
        return BaseResult.success(resultMap);
    }


    /**
     * 清空用户常用路线
     * @param mobile
     * @return
     */
    @RequestMapping(value = "/cleanUserRoute",method = RequestMethod.GET)
    @ResponseBody
    public Object cleanUserRoute(@RequestParam(value = "mobile",required = true)String mobile,String areacode){
        busRouteHistoryService.cleanBusRouteHistory(mobile,areacode);
        return BaseResult.success("操作成功！");
    }


    /**
     * 截取地址
     * @param regeocodesResult
     * @return
     */
    private String captureAddress(RegeocodesResult regeocodesResult) {
        AddressComponentResult componentResult = regeocodesResult.getAddressComponent();
        String province = componentResult.getProvince().toString();//省
        String city = componentResult.getCity().toString();//城市
        String district = componentResult.getDistrict().toString();//区县
        String township = componentResult.getTownship().toString();//街道
        String pcdtStr = province + city + district + township;
        String address = regeocodesResult.getFormattedAddress().replace(pcdtStr,"");
        return address;
    }
}